<?php
//旧网站跳转到首页
if(strstr($_SERVER['REQUEST_URI'],'index.php')){
	header("location:http://www.d2ziran.com/default/index.htm");
	exit;
}
//不带www访问地址
if($_SERVER['HTTP_HOST'] == 'd2ziran.com'){
	header("location:http://www.d2ziran.com".$_SERVER['REQUEST_URI']);
	exit;
}



require('ini_set.php');
	session_start();
$cf_module_array = array(//module目录
    'default' => MODULE_PATH . 'default/',
    'admin' => MODULE_PATH . 'admin/',
	'user' => MODULE_PATH . 'user/',
	'mobile' => MODULE_PATH . 'mobile/',
	'muser' => MODULE_PATH . 'muser/',
	'msp' => MODULE_PATH . 'msp/',
	'sp' => MODULE_PATH . 'sp/',
	'api' => MODULE_PATH . 'api/',	
);
$cf_module_key = array_keys($cf_module_array);	 //获取对应的键名
$site_url = $_SERVER['REQUEST_URI'];
//禁止XST与XSS攻击访问
if (strstr($site_url, 'script%3E') || strstr($site_url, 'script>') || strstr($site_url, '%3Ciframe') || strstr($site_url, '<iframe') || strstr($site_url, 'javascript:')) {
    header("HTTP/1.0 404 Not Found");
    echo "您输入的网址不存在!code:0";
    exit;
}

if (!strstr($site_url, '.htm')) {
	$site_url = rtrim($site_url, '/') . '/index.htm';	
}

$pathinfo = pathinfo($site_url);
$cf_dir = trim($pathinfo['dirname'],"\\");
if($cf_dir){								//是否根目录
	$cf_module = trim($cf_dir,"/");
}else{
	$cf_module = $cf_module_key[0];
}
$cf_action = 'index';
require(INC_PATH . 'function.php');

if (strstr($cf_module, '/')) {
	// echo $cf_module;
    header("HTTP/1.0 404 Not Found");
	tips(0,'您输入的网址不存在!');
    /*echo "您输入的网址不存在!code:2";
    exit;*/
}
	
if (!in_array($cf_module, $cf_module_key)) {										       //地址必须在模块内
    header("HTTP/1.0 404 Not Found");
    tips(0,'您输入的网址不存在!');
	//echo "输入的地址有误!code:3";
   // exit;

}
// dump(123);
$url_patten = "/^([a-z0-9_]+)(\-([a-z0-9]+)\_([a-z0-9]+))?((\_[a-z0-9\-]+\_[a-z0-9\-]+)*)?/i";
$filename = $pathinfo['filename'] ? $pathinfo['filename'] : substr($pathinfo['basename'], 0, strrpos($pathinfo['basename'], "."));
$get_page_info = preg_match($url_patten, $filename, $cf_page_ary);
if (!$get_page_info) {
    header("HTTP/1.0 404 Not Found");
    tips(0,'您输入的网址不存在!');
	
	//echo "您输入的网址不存在!code:4";
    //exit;
}
$cf_controller = $cf_page_ary[1];
if ($cf_page_ary[2]) {
    $key = $cf_page_ary[3];
    $_GET[$key] = $cf_page_ary[4];
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {      //数据发送类型 post get
    $cf_action = $_POST['act'] ? $_POST['act'] : $_GET['act'];
    $cf_ispost = '1';
} else {
    $cf_action = $_GET['act'] ? $_GET['act'] : $_POST['act'];
    $cf_ispost = '0';
}
if ($cf_page_ary[5]) {	    //变量处理
    $urlparam = explode("_", ltrim($cf_page_ary[5], "_"));
    $count = count($urlparam);
    for ($i = 0; $i < $count; $i+=2) {
		$key = $urlparam[$i];
		$val = isset($urlparam[$i + 1]) ? urldecode($urlparam[$i + 1]) : '';
		if($cf_ispost){
			$_POST[$key] = $val;
		}else{
			$_GET[$key] = $val;
		}
	
    }
}



$cf_action = $cf_action ? $cf_action : 'index';

/* * ***
  关键类库,加入类控制?
 * **** */

require(CONFIG_PATH . 'db_setting.php');
require(LIB_PATH . 'mysql_pdo_class.php');										      //pdo mysql
//require(LIB_PATH.'mysql_class.php');                                                //mysql

//连接数据库
$db = new MyPDO($cf_db_setting['host'], $cf_db_setting['user'], $cf_db_setting['pwd'], $cf_db_setting['dbname']);
//$db = new Mysql($cf_db_setting['host'],$cf_db_setting['user'],$cf_db_setting['pwd'],$cf_db_setting['dbname']);

//手机端跳转处理
if(is_mobile()){
	switch ($cf_module) {
		case 'default':
			$cf_module = 'mobile';
			break;
		case 'user':
			$cf_module = 'muser';
			break;
	}			
}



switch($cf_module){
	case "default":
		require(LIB_PATH . 'user_auth.php');
		require(LIB_PATH . 'cart_class.php');
		require(INFO_PATH . 'cf_siteconfig.php');
		require(CONFIG_PATH . 'seo_setting.php');	  //加载seo信息
		require(INFO_PATH . 'cf_article_cate.php');	  //加载分类信息

		$userauth = new UserAuth($db);
		$cf_user_info = $userauth->check_login();
		
		/*if(!$cf_user_info){
			$reurl = strstr($_SERVER["REQUEST_URI"], ".") ? $_SERVER["REQUEST_URI"] : rtrim($_SERVER["REQUEST_URI"], "/") . "/user/index.htm";
			$redirect = '?redirect=' . urlencode($reurl . "&ispost=0");
		}*/
	 
	break;
	case "user":
		require(LIB_PATH . 'user_auth.php');
		require(INFO_PATH . 'cf_siteconfig.php');
		require(CONFIG_PATH . 'seo_setting.php');	  //加载seo信息
		require(INFO_PATH . 'cf_article_cate.php');	  //加载分类信息
		$userauth = new UserAuth($db);
		$cf_user_info = $userauth->check_login();	
		//dump($cf_user_info);	
		if(!$cf_user_info){
			$reurl = strstr($_SERVER["REQUEST_URI"], ".") ? $_SERVER["REQUEST_URI"] :  "/user/index.htm";
			$redirect = '?redirect=' . urlencode($reurl . "&ispost=0");
			//die($redirect);
			header("location:".SITE_ADDR . 'default/user_login.htm' . $redirect);
		}

	break;
	case "admin":

		require(LIB_PATH . 'admin_auth.php');

		$adminauth = new AdminAuth($db);
		$adminauth->check_islogin();
		// var_dump($_SESSION);die;
// 		dump(543);
// dump($adminauth->check_islogin());
		// var_dump($adminauth->check_islogin());di
		if ($adminauth->check_islogin()) {
		load_file('power_group_class', LIB_PATH);
		$power_group = new PowerGroup($db);
		$where = "power_controller='" . $cf_controller . "' AND power_action='" . $cf_action . "'";
		$power_data = array('power_title' => $cf_controller . '-' . $cf_action,
			'power_controller' => $cf_controller,
			'power_action' => $cf_action);
		$action_file = $cf_module_array[$cf_module] . $cf_controller . '.php';
		$power_group->rebuild_power($where, $power_data, 1, $action_file);       //不停累加权限
		$check = $adminauth->check_power($cf_controller . '-' . $cf_action);
		if (!$check && 0) {
			//require(INC_PATH.'notallow.php');
			my_error('您没有此操作的权限!', '操作提示');
		}
	
		$cf_admin_info = $adminauth->get_admin_info();	
		load_file('menu_class', LIB_PATH);
		$menuob = new Menu($db);
		$menu_file = "var_menu" . $cf_admin_info['admin_gid'] . ".php";
		if (!is_file(INFO_PATH . $menu_file))
			$menuob->update_data_file($cf_admin_info['admin_gid']);
			$menu_array = $menuob->load_data($menu_file);
		
			$cf_admin_menu = $menu_array['1']['child'];	//頂部總菜單
			$cf_admin_more_menu = $menu_array['2']['child'];       //更多菜單
			$cf_admin_right_menu = $menu_array['3']['child'];       //右菜單
			$cf_dpt = isset($_GET['dpt']) ? intval($_GET['dpt']) : intval($_POST['dpt']);
			$cf_admin_dpt_row = $menuob->get_dpt_row($cf_admin_menu, $cf_dpt);
			$cf_admin_menu_on = $menuob->get_dpt_menu($cf_admin_dpt_row['child'], $cf_admin_dpt_row['menu_id']); //頂部選上菜單
		//history
		//load_file('history_class', LIB_PATH);
		//$history = new History($db);
		//$cf_history = $history->get_data_array("ht_uid='" . $cf_admin_info['admin_id'] . "' AND ht_act='add'", "ht_id DESC", "10");
	
		/*	 * ** 权限控制 ** */
		}else {

			if ($cf_controller != 'login') {
				//$reurl = SITE_ADDR.$cf_module.'/'.$cf_controller.'.htm';
				// $reurl .=$_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : '';
				$reurl = strstr($_SERVER["REQUEST_URI"], ".") ? $_SERVER["REQUEST_URI"] : rtrim($_SERVER["REQUEST_URI"], "/") . "/index.htm";

				$redirect = '?redirect=' . urlencode($reurl . "&ispost=" . $cf_ispost);
				header("location:".SITE_ADDR . 'admin/login.htm' . $redirect);

				//my_error('请登录后再进行操作', '信息提示', SITE_ADDR . 'admin/login.htm' . $redirect, 0, 'top');
				//exit;
			} else {				
				echo "other page";
				//exit;
			}
		}
	break;
	case "mobile":
		require(LIB_PATH . 'user_auth.php');
		require(LIB_PATH . 'cart_class.php');
		$userauth = new UserAuth($db);
		$cf_user_info = $userauth->check_login();	
	break;
	case "muser":
		require(LIB_PATH . 'user_auth.php');
		require(INFO_PATH . 'cf_siteconfig.php');
		require(CONFIG_PATH . 'seo_setting.php');	  //加载seo信息
		require(INFO_PATH . 'cf_article_cate.php');	  //加载分类信息
		$userauth = new UserAuth($db);
		$cf_user_info = $userauth->check_login();	
		//dump($cf_user_info);	
		if(!$cf_user_info){			
			$SESSION['my_jump'] = $_SERVER['REDIRECT_URL'];
			header("location:".SITE_ADDR . 'mobile/login.htm');
		}	
	break;
	case "sp":

	break;
	case "msp":
		if ($cf_controller != 'login') {
			//进行登录的控制
		} 
	break;
	case "api":	
	break;


	default:
		die("地址解析错误");
	break;
	
}

if($cf_module!='api'){
	require(LIB_PATH . 'smarty/Smarty.class.php');
	require(LIB_PATH . 'smarty/my_smarty_class.php');
	//require(LIB_PATH . 'session_db_class.php');
	//$session = new my_session($db);
	if ($_REQUEST['PHPSESSID']) {
		if($_POST['PHPSESSID']){
			session_id($_POST['PHPSESSID']);
		}elseif($_GET['PHPSESSID']){
			session_id($_GET['PHPSESSID']);
			//dump(session_id());
		}
	}
	

	
	$smarty = new MySmarty();	     	 
	$smarty->setTemplateDir($cf_module_array[$cf_module]);
	$smarty->setCompileDir($cf_module_array[$cf_module]. 'tmp/');
	$smarty->setCacheDir($cf_module_array[$cf_module]. 'cache/');
	$smarty->setModuleDir($cf_module_array[$cf_module]);		//自定义
	
	$smarty->left_delimiter = '{*';
	$smarty->right_delimiter = '*}';
	$smarty->setCompile_check(true);
	$smarty->caching = false;
	$smarty->debugging = false;
}





//$cf_path=MODULE_PATH.$cf_module.'/'.$cf_controller.'.php';							//引用文件
$cf_path = $cf_module_array[$cf_module] . $cf_controller . '.php';
$cf_module_path = SITE_ADDR . $cf_module . '/';									//当前所在模块地址
$cf_self_path = SITE_ADDR . $cf_module . '/' . $cf_controller;					//引有当前地址地址
$cf_self_page = $cf_self_path . '.htm';	  //引用当前页面
$cf_act_page = $cf_self_path . '-act_' . $cf_action . '.htm';					//本页表单发送
$cf_pre_page = $_SERVER['HTTP_REFERER'];
//$cf_url = $_SERVER['REQUEST_URI'];	//取得當前詳細地址
// dump(123);
/* * *smarty系统变量**** */
//require(INFO_PATH.'index_part.php');										//索引图
require(CONFIG_PATH . 'default_var.php');	  //变量smarty输出
	// dump($cf_path);
if (is_file($cf_path)) {
// echo 1;die;
    
    require($cf_path);
} else {
    header("HTTP/1.0 404 Not Found");
   /// tips(0,'页面不存在或禁止访问!');
	
	//die("此页面不存在或禁止访问");											    //模拟404错误
   my_error('此页面不存在','信息提示');
}


?>
